package com.caiheng.api.service.wxuser.impl;

import com.caiheng.api.dao.estate.EstateMapper;
import com.caiheng.api.dao.platform.ParameterMapper;
import com.caiheng.api.dao.wxuser.UserIntegralRecordMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.dao.wxuser.UserSignRecordMapper;
import com.caiheng.api.entity.estate.Estate;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.entity.wxuser.UserIntegralRecord;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.wxuser.UserIntegralRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserIntegralRecordServiceImpl extends BaseServiceImpl<UserIntegralRecordMapper, UserIntegralRecord> implements UserIntegralRecordService {

    @Resource
    private UserIntegralRecordMapper userIntegralRecordMapper;
    @Resource
    private UserSignRecordMapper userSignRecordMapper;
    @Resource
    private ParameterMapper parameterMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private EstateMapper estateMapper;

    @Override
    public List<UserIntegralRecord> selectByUserId(Integer user_id, Integer offset, Integer pageSize) {
        return userIntegralRecordMapper.selectByUserId(user_id,offset,pageSize);
    }

    @Override
    public int getSignTotalIntegral(Integer user_id, Date expire_time,Integer in_or_out) {
        return userIntegralRecordMapper.getSignTotalIntegral(user_id,expire_time,in_or_out);
    }

    /**
     * 获取当前用户  所剩余的 所有积分
     * @param user_id
     * @return
     */
    @Override
    public int getTotalIntegral(Integer user_id) {
        //积分的获取类型   1 - 系统赠送  2 - 保单获取  3 - 签到获取  4 - 购买积分商品消费
//        int userPolicyIntegral = userPolicyDao.getUserPolicyTotalIntegral(user_id,new Date());//保单兑换的积分
        int policyIntegral = userIntegralRecordMapper.getSignTotalIntegral(user_id,new Date(),2);//签到 积分  增加积分
        int signIntegral = userIntegralRecordMapper.getSignTotalIntegral(user_id,new Date(),3);//签到 积分  增加积分
        int costIntegral = userIntegralRecordMapper.getSignTotalIntegral(user_id,new Date(),4);//消费积分
        return policyIntegral+signIntegral+costIntegral;
    }

    /**
     * 积分消费
     * @param user
     * @param useYdCount
     * @return
     */
    @Transactional
    @Override
    public int costIntegral(User user, Integer estate_id, Double useYdCount, String remark) {
        int ret = 0;
        try{
//            double integral = user.getIntegral();
//            double newIntegral = integral - useYdCount;
//            user.setIntegral(newIntegral);

//            ret = userMapper.updateById(user);
//            if(ret <= 0){
//                throw new TransactionalException("integralRecord insert error");
//            }


            //业主用户分成
            UserIntegralRecord integralRecord = new UserIntegralRecord();
            integralRecord.setPayNo(String.valueOf(new Date().getTime()));
            integralRecord.setPropertyId(user.getPropertyId());
            integralRecord.setEstateId(estate_id);

            integralRecord.setPhoneNum(user.getPhoneNum());
            Estate estate = estateMapper.selectById(estate_id);
            if(estate!=null){
                integralRecord.setEstateName(estate.getEstateName());
            }

            //积分的获取类型   1 - 购买商品、商家服务获取  2 -- 缴纳物业费使用
            integralRecord.setState(1);
            integralRecord.setNickName(user.getNickName());
            integralRecord.setGetType(3);//积分的获取类型   1 - 购买商品、商家服务获取  2 -- 缴纳物业费使用  3 - 单独支付物业费（使用跃豆）
            integralRecord.setUserId(user.getId());
            integralRecord.setCreateTime(new Date());
            integralRecord.setInOrOut(0);//增加积分  还是 减少积分  0 - 减少  1 - 加
            integralRecord.setRemark("用户扫码直接支付跃豆");
            integralRecord.setIntegral(useYdCount);
            integralRecord.setUserRemark(remark);
            ret = userIntegralRecordMapper.insert(integralRecord);
            if(ret<=0){
                throw new TransactionalException("userIntegralRecordMapper insert error");
            }
            return ret;
        }catch (Exception ex){
            ex.printStackTrace();
            throw new TransactionalException("integralRecord insert error");
        }
    }

}
